﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hdt.TransOcean.Dal;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using Hdt.TransOcean.Presentation.UserControls;
using DevExpress.XtraEditors.Repository;
using System.Data;
using DevExpress.XtraEditors;

namespace Hdt.TransOcean.Presentation
{
    class Presentation
    {
        #region "Airline"

        public static void GetAirline(ref Airline airline, string code, string name, string description)
        {
            airline.code = code;
            airline.name = name;
            airline.description = description;
        }


        public static bool ValidAirline(Airline airline)
        {
            string message = string.Empty;

            if (airline.code.Equals(string.Empty))
            {
                message = "Chưa nhập mã";
            }
            if (airline.name.Equals(string.Empty))
            {
                if(message.Equals(string.Empty))
                    message += "\nChưa nhập tên";
                else
                    message += "\nChưa nhập tên";
            }
            if (message != string.Empty)
            {
                MessageBox.Show(message, "Hãng hàng không");
                return false;
            }
            return true;
        }




        #endregion

        public static bool ValidTermBeginningDebtDetail(TermBeginningDebt invoiceDetail)
        {
            string message = string.Empty;
            return true;
        }

        public static bool ValidCustomer(Customer customer)
        {
            string message = string.Empty;

            if (customer.code.Equals(string.Empty))
            {
                message = "Chưa nhập mã";
            }
            if (customer.name.Equals(string.Empty))
            {
                if (message.Equals(string.Empty))
                    message += "\nChưa nhập tên";
                else
                    message += "\nChưa nhập tên";
            }
            if (message != string.Empty)
            {
                MessageBox.Show(message, "Khách hàng");
                return false;
            }
            return true;
        }

        public static bool ValidItem(Item item)
        {
            string message = string.Empty;


            return true;
        }

        public static bool ValidJourney(Journey journey)
        {
            string message = string.Empty;

            if (journey.code.Equals(string.Empty))
            {
                message = "Chưa nhập mã";
            }
            if (journey.name.Equals(string.Empty))
            {
                if (message.Equals(string.Empty))
                    message += "\nChưa nhập tên";
                else
                    message += "\nChưa nhập tên";
            }
            if (message != string.Empty)
            {
                MessageBox.Show(message, "Hành trình");
                return false;
            }
            return true;
        }

        public static bool ValidInvoice(Invoice invoice, InvoiceDetail[] invoiceDetail)
        {
            string message = string.Empty;

            if (invoice.code.Equals(string.Empty))
            {
                message += "\nChưa nhập mã hóa đơn";
            }
            if (invoice.created_date.Equals(null))
            {
                message += "\nChưa nhập lập hóa đơn";
            }

            return true;
        }
        public static bool ValidInvoice(Invoice saleInvoice)
        {
            string message = string.Empty;


            return true;
        }

        public static bool ValidEmployee(Employee employee)
        {
            string message = string.Empty;

            if (employee.code.Equals(string.Empty))
            {
                message = "Chưa nhập mã";
            }
            if (employee.name.Equals(string.Empty))
            {
                if (message.Equals(string.Empty))
                    message += "\nChưa nhập tên";
                else
                    message += "\nChưa nhập tên";
            }
            if (message != string.Empty)
            {
                MessageBox.Show(message, "Nhân viên");
                return false;
            }
            return true;
        }

        public static bool ValidInvoiceDetail(InvoiceDetail invoiceDetail)
        {
            string message = string.Empty;


            return true;
        }

        public static void BindGridView(EObject obj, GridControl grid)
        {
            Dao dao = new Dao();
            BindingSource bsDataSource = new BindingSource();

            switch (obj)
            { 
                case EObject.Airline:

                    break;
                           
            
            }
            var airlines = Dao.SelectAirline();
            bsDataSource.DataSource = airlines;
            grid.DataSource = bsDataSource;
            //gridColumnCode.FieldName = "code";
            //gridColumnName.FieldName = "name";
            //gridColumnDescription.FieldName = "description";
        }


        public static void BindSaleInvoiceDetailGrid(GridControl grid, string invoiceCode)
        {
            Dao dao = new Dao();
            BindingSource bsDataSource = new BindingSource();

            
            var airlines = dao.SelectSaleInvoiceDetail(invoiceCode);
            bsDataSource.DataSource = airlines;
            grid.DataSource = bsDataSource;
            //gridColumnCode.FieldName = "code";
            //gridColumnName.FieldName = "name";
            //gridColumnDescription.FieldName = "description";
        }

        public static void OpenForm(Form mdiForm, string formName, EOperation operation, string code = null)
        {
            //Form form;
            switch (formName)
            {
                case "FormCustomer":
                    FormCustomer formCustomer = Application.OpenForms[formName] as FormCustomer;
                    if (formCustomer == null)
                    {
                        formCustomer = new FormCustomer(operation, code);
                        formCustomer.MdiParent = mdiForm;
                        formCustomer.Show();
                    }
                    else
                    {
                        formCustomer.Close();
                        formCustomer.Dispose();
                        formCustomer = new FormCustomer(operation, code);
                        formCustomer.MdiParent = mdiForm;
                        formCustomer.Show();
                        formCustomer.Activate();
                    }
                    break;
                case "FormAirline":
                    FormAirline formAirline = Application.OpenForms[formName] as FormAirline;
                    if (formAirline == null)
                    {
                        formAirline = new FormAirline(operation, code);
                        formAirline.MdiParent = mdiForm;
                        formAirline.Show();
                    }
                    else
                    {
                        formAirline.Close();
                        formAirline.Dispose();
                        formAirline = new FormAirline(operation, code);
                        formAirline.MdiParent = mdiForm;
                        formAirline.Show();
                        formAirline.Activate();
                    }
                    break;

                case "FormInvoice":
                    FormInvoice frmInvoice = Application.OpenForms[formName] as FormInvoice;
                    if (frmInvoice == null)
                    {
                        frmInvoice = new FormInvoice(operation, code);
                        frmInvoice.MdiParent = mdiForm;
                        frmInvoice.Show();
                    }
                    else
                    {
                        frmInvoice.Close();
                        frmInvoice.Dispose();
                        frmInvoice = new FormInvoice(operation, code);
                        frmInvoice.MdiParent = mdiForm;
                        frmInvoice.Show();
                        frmInvoice.Activate();
                    }
                    break;

                case "FormItem":
                    FormItem formItem = Application.OpenForms[formName] as FormItem;
                    if (formItem == null)
                    {
                        formItem = new FormItem(operation, code);
                        formItem.MdiParent = mdiForm;
                        formItem.Show();
                    }
                    else
                    {
                        formItem.Close();
                        formItem.Dispose();
                        formItem = new FormItem(operation, code);
                        formItem.MdiParent = mdiForm;
                        formItem.Show();
                        formItem.Activate();
                    }
                    break;

                case "FormJourney":
                    FormJourney formJourney = Application.OpenForms[formName] as FormJourney;
                    if (formJourney == null)
                    {
                        formJourney = new FormJourney(operation, code);
                        formJourney.MdiParent = mdiForm;
                        formJourney.Show();
                    }
                    else
                    {
                        formJourney.Close();
                        formJourney.Dispose();
                        formJourney = new FormJourney(operation, code);
                        formJourney.MdiParent = mdiForm;
                        formJourney.Show();
                        formJourney.Activate();
                    }
                    break;
                case "FormEmployee":
                    FormEmployee formEmployee = Application.OpenForms[formName] as FormEmployee;
                    if (formEmployee == null)
                    {
                        formEmployee = new FormEmployee(operation, code);
                        formEmployee.MdiParent = mdiForm;
                        formEmployee.Show();
                    }
                    else
                    {
                        formEmployee.Close();
                        formEmployee.Dispose();
                        formEmployee = new FormEmployee(operation, code);
                        formEmployee.MdiParent = mdiForm;
                        formEmployee.Show();
                        formEmployee.Activate();
                    }
                    break;

            }
        }

        public static void ViewControl(Control form, EOperation operation)
        {
            if (operation == EOperation.New)
            { 
                
                foreach (Control childControl in form.Controls)
                {
                    childControl.Enabled = false;
                }
            }
        }

        public static void DisplayButtonByOperation(HdtManageControl hdtControl, EOperation operation)
        {
            switch (operation)
            { 
                case EOperation.New:
                    hdtControl.EnabledNewButton = false;
                    hdtControl.EnabledEditButton = false;
                    hdtControl.EnabledDeleteButton = false;
                    hdtControl.EnabledSaveButton = true;
                    break;
                case EOperation.Edit:
                    hdtControl.EnabledEditButton = false;
                    hdtControl.EnabledNewButton = true;
                    hdtControl.EnabledDeleteButton = true;
                    hdtControl.EnabledSaveButton = true;
                    break;
                case EOperation.Save:
                    hdtControl.EnabledNewButton = true;
                    hdtControl.EnabledEditButton = false;
                    hdtControl.EnabledSaveButton = false;
                    break;
                
            
            }
        }
        public static void DisplayButtonByFocusedRowHandle(HdtManageControl hdtControl, int focusedRowHandle)
        {
            if (focusedRowHandle >= 0)
            {
                hdtControl.EnabledDeleteButton = true;
                hdtControl.EnabledEditButton = true;
            }
            else
            {
                hdtControl.EnabledDeleteButton = false;
                hdtControl.EnabledEditButton = false;
            }
        }

        public static void BindGridLookupEdit(RepositoryItemGridLookUpEdit repositoryItemGridLookUpEdit, DataTable dt)
        {
            repositoryItemGridLookUpEdit.DataSource=dt;
            repositoryItemGridLookUpEdit.ValueMember="code";
            repositoryItemGridLookUpEdit.DisplayMember="name";
        }
        public static void BindGridLookupEdit(GridLookUpEdit gridLookUpEdit, DataTable dt)
        {
            gridLookUpEdit.Properties.DataSource = dt;
            gridLookUpEdit.Properties.ValueMember = "code";
            gridLookUpEdit.Properties.DisplayMember = "name";
        }
        public static DataTable GetData(EObject eOjbect)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("code", typeof(Int32));
            dt.Columns.Add("name", typeof(string));

            switch (eOjbect)
            {
                case EObject.CustomerType:
                    dt.Rows.Add(1, "Khách quen");
                    dt.Rows.Add(2, "Đại lý");
                    dt.Rows.Add(3, "Vãng lai");
                    break;
                case EObject.JourneyType:
                    dt.Rows.Add(1, "Trong nước");
                    dt.Rows.Add(2, "Quốc tế");
                    break;
                case EObject.Department:
                    dt.Rows.Add(1, "Kinh doanh");
                    dt.Rows.Add(3, "Bán hàng");
                    dt.Rows.Add(2, "Kế toán");
                    
                        break;
                case EObject.Title:
                    dt.Rows.Add(1, "Nhân viên");
                    dt.Rows.Add(2, "Phó phòng");
                    dt.Rows.Add(3, "Trưởng phòng");
                        break;
            }
            return dt;
        }

        
    }
}

